package com.tcu.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tcu.user.entity.Permission;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

@Mapper
public interface PermissionMapper extends BaseMapper<Permission> {
    /**
     * role 和permission 表的关系
*/

    @Insert("insert into sbn_role_permission values(#{roleId},#{permissionId})")
    Integer addPermissionForRole(String roleId,String permissionId);

    @Delete("delete from sbn_role_permission where role_id = #{roleId}")
    Integer deletePermissionIdsByRid(String roleId);

    @Select("select permission_id from sbn_role_permission where role_id = #{roleId}")
    List<String> findPermissionIdsByRid(String roleId);

    @Select("select p.permission_name from sbn_permission p where p.id in (select rp.permission_id from sbn_role_permission rp where rp.role_id = #{roleId} )")
    Set<String> getPermissionNamesByRid(String roleId);

    @Select("select p.* from sbn_permission p where p.id in (select rp.permission_id from sbn_role_permission rp where rp.role_id = #{roleId} )")
    Set<Permission> getPermissionsByRid(String roleId);

    @Select("select distinct p.permission_name from sbn_permission p\n" +
            "where p.id in (select rp.permission_id from sbn_role_permission rp\n" +
            "where rp.role_id in (select ur.role_id from sbn_user_role ur where ur.user_id = #{userId}))")
    Set<String> getPermissionNamesByUid(String userId);

}
